
"""
Define base functions to compare resources.

"""

def value_compare(a,b):
	""" Compares two resources 'a' and 'b'.
			The objects describing the resources are lists
			@param a: first object
			@type a: list
			@param b: second object
			@type b:list
			@return: 0  if a is equal to b
					 1  if a < b
					 2  if a > b
					 -1 if a and b are not comparable
	"""
	score = 0
	for xi in range(len(a)):
		if a[xi] == b[xi]: continue
		elif a[xi] < b[xi]:
			if score == 1: continue
			elif not score: score = 1
			else: return -1
		else:
			if score == 2: continue
			elif not score: score = 2
			else: return -1
	return score

def value_compare_dict(a,b):
	""" Compares two resources 'a' and 'b'.
			The objects describing the resources are dico
			@param a: first object
			@type a: list
			@param b: second object
			@type b:list
			@return: 0  if a is equal to b
					 1  if a < b
					 2  if a > b
					 -1 if a and b are not comparable
	"""
	score = 0
	for xi in a.keys():
		if a[xi] == b[xi]: continue
		elif a[xi] < b[xi]:
			if score == 1: continue
			elif not score: score = 1
			else: return -1
		else:
			if score == 2: continue
			elif not score: score = 2
			else: return -1
	return score

		
# Implemente l'operateur de comparaison du poset, associe a l ensemble des ressources (aka profils)
def compare(a,b):
    """ Compares two resources 'a' and 'b'.
            The objects describing the resources may be dictionaries or lists
            @param a: first object
            @type a: dict or list
            @param b: second object
            @type : dict or list
            @return: 0  if a is equal to b
                     1  if a < b
                     2  if a > b
                     -1 if a and b are not comparable
            
            see also value_compare
    """
    a_d = (type(a) == type(dict()))
    b_d = (type(b) == type(dict()))
    if (a_d and not b_d) or  ( b_d and not a_d): raise Exception("compare")
    if a_d and b_d: return value_compare_dict(a,b)
    else: return value_compare(a,b)


##    if type(a) == type(dict()): return value_compare_dict(a,b)
##    else: return 
##		
##	if type(a) == type(dict()): 
##		if type(b) == type(dict()):
##			# variable attributes :)
##			#k = [kk for kk in a.keys() if kk in b.keys() and a[kk] !="" and b[kk] != ""] 
##			k = a.keys()
##			k.sort()
##			a2 = [a[x] for x in k]
##			b2 = [b[x] for x in k]
##			return value_compare(a2,b2)
##		else:
##			k = a.keys()
##			k.sort()
##			a2 = [a[x] for x in k]
##			return value_compare(a2,b)
##	else:
##		return value_compare(a,b)
